package com.rr.singleton;

//懒汉式
public class LazyMan3 {

    //私有的静态属性
    private static LazyMan3 lazyMan3;

    //私有的构造方法
    private LazyMan3(){
        System.out.println(Thread.currentThread().getName()+"ok");
    }

    //公共的静态入口
    public static LazyMan3 getInstance(){
        //加锁
        if(lazyMan3==null){
            synchronized (LazyMan3.class){
                if(lazyMan3==null){
                    lazyMan3=new LazyMan3();//不是原子性操作
                    //1.分配内存空间
                    //2.执行构造方法,初始化对象
                    //3.把对象分配给空间
                }
            }
        }
        return lazyMan3;
    }

    public static void main(String[] args) {
        new Thread(()->{
            LazyMan3.getInstance();
        }).start();
    }
}
